import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_mine_uestion.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/home/new/study_item_widget.dart';
import 'package:flutter_learn_vi/src/page/mine/mine_my_exam_page.dart';
import 'package:flutter_learn_vi/src/page/mine/question_model.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021466098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class MainExamPage extends StatefulWidget {
  @override
  _MainExamPageState createState() => _MainExamPageState();
}

class _MainExamPageState extends State<MainExamPage> with MineQuestionModel {
  StudyModelBean _studyModelBean;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration.zero, () {
      initData();
    });
  }

  void initData() async {
    _studyModelBean = await getStudyModel(context: context);
    _isLoading = false;
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("学习"),
        centerTitle: true,
        elevation: 1,
      ),
      backgroundColor: Colors.white,

      ///填充布局
      body: Container(
          width: double.infinity,
          height: double.infinity,
          child: Stack(
            children: [
              // Positioned.fill(
              //   child: buildBackground(),
              // ),
              SingleChildScrollView(
                physics: BouncingScrollPhysics(),
                child: Column(
                  children: [
                    Hero(
                      tag: "pcTest",
                      child: Material(
                        child: buildItemContainer(
                            imagePath: "assets/images/2.0x/pc_exam_icon.png",
                            text: "考试模式",
                            color: Color(0xff1b83c9),
                            bottomText: "同步电脑端的机考功能...",
                            click: () {
                              NavigatorUtils.pushPage(
                                  context, MineMyExamPage());
                            }),
                        color: Colors.transparent,
                      ),
                    ),
                    Container(
                      child: buildStudyGridClass(),
                      padding: EdgeInsets.all(16),
                    ),
                  ],
                ),
              ),
            ],
          )),
    );
  }

//第一部分 第一层 渐变背景
  Container buildBackground() {
    return Container(
      decoration: BoxDecoration(
        //线性渐变
        gradient: LinearGradient(
          //渐变角度
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          //渐变颜色组
          colors: [
            Colors.lightBlue.withOpacity(0.3),
            Colors.lightBlueAccent.withOpacity(0.3),
            Colors.blue.withOpacity(0.3),
          ],
        ),
      ),
    );
  }

  Widget buildItemContainer(
      {String imagePath,
      String text,
      Color color,
      Function click,
      String bottomText}) {
    return InkWell(
      onTap: () {
        if (click != null) {
          click();
        }
      },
      child: Container(
        margin: EdgeInsets.all(20),
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(boxShadow: [
          BoxShadow(
              color: color.withOpacity(0.4),
              spreadRadius: 1,
              blurRadius: 2,
              offset: Offset(1, 1)),
        ], color: color, borderRadius: BorderRadius.all(Radius.circular(10))),
        height: 120,
        child: Column(
          children: [
            Row(
              children: [
                Image.asset(
                  imagePath,
                  width: 38,
                ),
                SizedBox(
                  width: 20,
                ),
                Expanded(
                  child: Text(
                    "$text",
                    style: TextStyle(
                      fontWeight: FontWeight.w500,
                      color: Colors.white,
                      fontSize: 20,
                    ),
                  ),
                ),
                SizedBox(
                  width: 40,
                ),
              ],
            ),
            SizedBox(
              height: 22,
            ),
            Row(
              children: [
                Expanded(
                  child: Text(
                    bottomText,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(fontSize: 12, color: Colors.white),
                  ),
                ),
                Image.asset(
                  "assets/images/2.0x/arror_right.png",
                  width: 14,
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  bool _isLoading = true;

  buildStudyGridClass() {
    if (_isLoading) {
      return LoadingWidget();
    }
    if (_studyModelBean == null) {
      return NoDataWidget(
        clickCallBack: () {
          initData();
        },
      );
    }

    return GridView.count(
      crossAxisCount: 3,
      children: buildLeftWidget(),
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
    );
  }

  buildStudyClass() {
    if (_isLoading) {
      return LoadingWidget();
    }
    if (_studyModelBean == null) {
      return NoDataWidget(
        clickCallBack: () {
          initData();
        },
      );
    }
    return Row(
      children: [
        Expanded(
          child: Column(
            children: buildLeftWidget(),
          ),
        ),
        Expanded(
          child: Column(
            children: [
              SizedBox(
                height: 14,
              ),
              StudyItemWidget(
                _studyModelBean.studyModelItemList[6],
                radius: 46,
                width: 70,
              ),
              SizedBox(
                height: 54,
              ),
              StudyItemWidget(
                _studyModelBean.studyModelItemList[7],
                radius: 46,
                width: 70,
              ),
            ],
          ),
        ),
        Expanded(
          child: Column(
            children: [
              SizedBox(
                height: 14,
              ),
              StudyItemWidget(
                _studyModelBean.studyModelItemList[1],
                radius: 30,
                width: 46,
              ),
              SizedBox(
                height: 54,
              ),
              StudyItemWidget(
                _studyModelBean.studyModelItemList[3],
                radius: 30,
                width: 46,
              ),
              SizedBox(
                height: 54,
              ),
              StudyItemWidget(
                _studyModelBean.studyModelItemList[5],
                radius: 30,
                width: 46,
              ),
            ],
          ),
        ),
      ],
    );
  }

  buildLeftWidget() {
    List<StudyModelItemBean> list = _studyModelBean.studyModelItemList;
    LogUtils.e("list大小 ${list.length}");
    int index = 0;
    List<Widget> widgetList = [];
    for (int i = 0; i < list.length; i++) {
      widgetList.add(
        StudyItemWidget(
          _studyModelBean.studyModelItemList[i],
          radius: 30,
          width: 46,
        ),
      );
    }

    return widgetList;
  }
}
